package com.example.libraryapp.dialog.personal;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.libraryapp.R;
import com.example.libraryapp.dialog.DiyDialog;
import com.example.libraryapp.util.SexUtil;
import com.example.libraryapp.util.ToastUtil;

/**
 * @author 伍凯铭
 * @since 2020/8/3
 */
public class SexDialog implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private DiyDialog dialog;

    private OnPersonalListener mOnPersonalListener;

    private RadioButton[] mButtonArray;

    public SexDialog(Context context, OnPersonalListener onPersonalListener) {
        this.mOnPersonalListener = onPersonalListener;
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_sex, null);
        dialog = new DiyDialog(context, view);
        view.findViewById(R.id.cancel_btn).setOnClickListener(this);
        mButtonArray = new RadioButton[]{view.findViewById(R.id.secrecy_btn),view.findViewById(R.id.man_btn),view.findViewById(R.id.woman_btn)};
        RadioGroup group = view.findViewById(R.id.sex_group);
        group.setOnCheckedChangeListener(this);
    }

    public void init(Integer sex) {
        if(sex == null) {
            mButtonArray[0].setChecked(true);
        }else {
            mButtonArray[sex].setChecked(true);
        }
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    public void dismiss(){
        dialog.closeDiyDialog();
    }

    public void show() {
        dialog.showDiyDialog();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.man_btn:
                mOnPersonalListener.onSexSure(SexUtil.MAN);
                break;
            case R.id.woman_btn:
                mOnPersonalListener.onSexSure(SexUtil.WOMAN);
                break;
            case R.id.secrecy_btn:
                mOnPersonalListener.onSexSure(SexUtil.SECRECY);
                break;
        }
        dismiss();
    }
}
